package com.xjtu.platform.user.consultor.consultRecord.impl;

import com.xjtu.platform.common.Constants;
import com.xjtu.platform.common.Utils;
import com.xjtu.platform.user.consultor.consultRecord.ConsultPageParam;
import com.xjtu.platform.user.consultor.consultRecord.ConsultRecordDto;
import com.xjtu.platform.user.consultor.consultRecord.ConsultRecordService;
import com.xjtu.platform.user.consultor.consultRecord.ExcelModel;
import com.xjtu.platform.user.consultor.consultRecord.dao.ConsultRecordDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @创建人 MBY381
 * @创建时间 2022/7/3 09:29
 * @描述 暂无
 */


@Service
@Transactional
public class ConsultRecordServiceImpl implements ConsultRecordService {


    @Resource
    private ConsultRecordDao consultRecordDao;


    @Override
    public Map<String, Object> getListByConsultantId(ConsultPageParam consultant_id) {
        System.out.println("正在获取 " + consultant_id + " 的咨询记录");
        return Utils.getPage(consultant_id, () -> consultRecordDao.getList(consultant_id.getUser_id()));
    }

    @Override
    public void updateStateAndInfo(ConsultRecordDto dto) {
        Date date=new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateNowStr = sdf.format(date);
        dto.setStart_time(dateNowStr+" "+dto.getStart_time());
        dto.setEnd_time(dateNowStr+" "+dto.getEnd_time());
        consultRecordDao.updateInfo(dto);
        consultRecordDao.activateNext(dto);
    }

    @Override
    public void endUpConsult(ConsultRecordDto dto) {
        consultRecordDao.endUpVisit(dto);
        consultRecordDao.endUpConsult(dto);
    }

    @Override
    public String makeXlsxOutFiles(String visit_id) {
        List<ExcelModel> data = consultRecordDao.getListForFile(visit_id);
        System.out.println(data.get(0).getComment());
        String path = visit_id+"-"+consultRecordDao.findStudentID(visit_id)+"-consult";
        Utils.xlsFileExecutor(data,new String[]{"项目ID","学生ID","开始时间","结束时间","地点","结论","轮次","状态"},path);
        path=Constants.FILE_DIR+path+".xls";
        consultRecordDao.updatePath(visit_id, path);
        System.out.println("IMPL拿到的path是:"+path);
        return path;
    }

    @Override
    public String findPath(String visit_id) {
        return consultRecordDao.findPath(visit_id);
    }

}
